package com.jiangxiaofei.citycloud.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.citycountry.FPCJAXActivity;
import com.jiangxiaofei.citycloud.activity.citycountry.LLKFYHActivity;
import com.jiangxiaofei.citycloud.activity.citycountry.LLWTYYActivity;
import com.jiangxiaofei.citycloud.activity.fleamarket.FleaMarketHomeActivity;
import com.jiangxiaofei.citycloud.activity.freshfruits.FreshFruitsHomeActivity;
import com.jiangxiaofei.citycloud.activity.park.ParkMapAcitivity;
import com.jiangxiaofei.citycloud.activity.recruit.RecruitMapAcitivity;
import com.jiangxiaofei.citycloud.activity.village.VillageAcitivity;
import com.jiangxiaofei.citycloud.adapter.HomeMenuAdapter;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.HomeMenuBean;
import com.jiangxiaofei.citycloud.constants.BannerId;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.util.BannerViewHolder;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * @author JiangXFei
 * @content 社区便民
 */

public class CityCountryFrament extends Fragment implements MainConstant.GetBannerView {

    @InjectView(R.id.recy_home_menu)
    RecyclerView recyHomeMenu;
    private View cityView;

    private List<HomeMenuBean> menuList = new ArrayList<>();
    private BaseQuickAdapter homeAdapter;
    private static final String[] TITLE = {
            "二手物资交流",
            "生鲜果蔬直供",
            "劳务服务",
            "停车充电洗车",
            "美丽乡村",
            "社区邻里",
            "理疗康复医患",
            "田间采摘农宴",
            "扶贫公益爱心"};
    private static final int[] IMG = {
            R.drawable.ic_11_eswzjl,   //二手物资
            R.drawable.ic_11_sxgszg,   //生鲜水鬼
            R.drawable.ic_11_lwfw,     //劳务服务
            R.drawable.ic_11_tccd,     //停车充电
            R.drawable.ic_11_tycz,   //美丽乡村
            R.drawable.ic_11_sqll,     //社区邻里
            R.drawable.ic_11_llkfyh,     //理疗康复
            R.drawable.ic_11_fpgy};    //扶贫公益爱心
    private static final Class<?>[] ACTIVITIES = {
            FleaMarketHomeActivity.class,    //二手物资
            FreshFruitsHomeActivity.class,   //生鲜水鬼
            RecruitMapAcitivity.class,       //劳务服务
            ParkMapAcitivity.class,          //停车充电
            VillageAcitivity.class,            //美丽乡村
            LLWTYYActivity.class,            //社区邻里
//            LiveActivity.class,
            LLKFYHActivity.class,            //理疗康复
            FPCJAXActivity.class};           //扶贫公益爱心
    private Context mContext;
    private Activity mActivity;
    private List<String> imgsList = new ArrayList<>();
    private MZBannerView bannerView;
    private MainConstant.GetBannerPersenter getBannerPersenter;
    private BannerViewHolder bannerViewHolder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle
            savedInstanceState) {
        cityView = inflater.inflate(R.layout.fragment_city_layout, null);
        ButterKnife.inject(this, cityView);
        mContext = getActivity();
        mActivity = getActivity();
        bannerViewHolder = new BannerViewHolder(mContext);
        initAdapter();
        initMenuData();
        getBannerPersenter = new MainPersenter(mContext, (MainConstant.GetBannerView) this);
        getBannerPersenter.getBanner();
        return cityView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    /**
     * 初始化菜单数据
     */
    private void initMenuData() {
        if (menuList.size() > 0) {
            menuList.clear();
        }
        for (int i = 0; i < 6; i++) {
            HomeMenuBean bean = new HomeMenuBean();
            bean.setTitle(TITLE[i]);
            bean.setImgRes(IMG[i]);
            bean.setActivityClass(ACTIVITIES[i]);
            menuList.add(bean);
        }
        homeAdapter.notifyDataSetChanged();
    }

    /**
     * 初始化Adapter
     */
    private void initAdapter() {
        recyHomeMenu.setLayoutManager(new GridLayoutManager(mContext, 2));
        homeAdapter = new HomeMenuAdapter(R.layout.item_home_view, menuList);
        View header = LayoutInflater.from(mContext).inflate(R.layout.header_banner_view, null);
        bannerView = header.findViewById(R.id.banner);
        homeAdapter.addHeaderView(header);
        homeAdapter.setOnItemClickListener((adapter, view, position) -> {
            startActivity(new Intent(mContext, ACTIVITIES[position]));
        });
        recyHomeMenu.setAdapter(homeAdapter);
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getBannerParam() {
        return BannerId.CITYHOMEID;
    }

    @Override
    public void bannerSuccess(List<BannerBean> bannerBeans) {
        bannerView.setPages(bannerBeans, bannerViewHolder);
        bannerView.start();
    }

}
